import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/api/test.dart' as TestApi;
import 'package:flutter_application_1/components/loading_tip/loading_tip.dart';
import 'package:flutter_application_1/request/request.dart';
import 'package:flutter_application_1/request/upload.dart';
import 'package:flutter_application_1/utils/choose_assets.dart';
import 'package:flutter_application_1/utils/preview_images.dart';
import 'package:flutter_application_1/utils/utils.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:wechat_camera_picker/wechat_camera_picker.dart';

class RequestTest extends StatefulWidget {
  @override
  _RequestTestState createState() => _RequestTestState();
}

class _RequestTestState extends State<RequestTest> {
  List<String> _uploadSrcs = [
    'https://i1.hdslb.com/bfs/face/27956154ebc30ab1de758b6ad53daee844a2e42c.jpg@160w_160h_1c.jpg',
  ];

  // 预览图片
  void _preview(int currentIndex) {
    PreviewImages.preview(
      context: context,
      urls: _uploadSrcs,
      currentIndex: currentIndex,
    );
  }

  @override
  void dispose() {
    LoadingTip.remove();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('网络请求'),
      ),
      body: SingleChildScrollView(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Column(
              children: [
                ElevatedButton(
                  child: Text('test get'),
                  onPressed: () {
                    LoadingTip.show(context: context);
                    TestApi.testGet({
                      'cartId': '11',
                      'goodsId': '22',
                      'name': '红米手机 K30 Pro',
                      'price': 2999.00,
                      'number': 1
                    }).then((value) {
                      Fluttertoast.showToast(msg: '需打开开发工具查看控制台');
                      print(value);
                      print(value.data?.name);
                    }).whenComplete(() {
                      LoadingTip.remove();
                    });
                  },
                ),
                TextButton(
                  child: Text('test post'),
                  onPressed: () {
                    LoadingTip.show(context: context);
                    TestApi.testPost({
                      'userId': '777',
                      'gender': 1,
                      'avatar':
                          'https://thirdwx.qlogo.cn/mmopen/vi_32/ZCkthDMhtd7a0AmPd0SZ36zqeGqKwKGj36oiaO9pjcYy4EMDWbbelTKe6GajpNJHlpDb2x9ScnXvsONBE5ibrR4A/132',
                      'status': 1,
                      'username': 'fanzhiliang',
                      'password': '',
                      'phone': '13128269543',
                      'createTime': 1622444287
                    }).then((value) {
                      Fluttertoast.showToast(msg: '需打开开发工具查看控制台');
                      print(value);
                      print(value.data?.phone);
                    }).whenComplete(() {
                      LoadingTip.remove();
                    });
                  },
                ),
                ElevatedButton(
                  child: Text('test header'),
                  onPressed: () {
                    LoadingTip.show(context: context);
                    TestApi.testHeader({
                      'loginKey': 'loginKey123',
                    }).then((value) {
                      Fluttertoast.showToast(msg: '需打开开发工具查看控制台');
                      print(value.data);
                    }).whenComplete(() {
                      LoadingTip.remove();
                    });
                  },
                ),
                TextButton(
                  child: Text('test error'),
                  onPressed: () {
                    LoadingTip.show(context: context);
                    TestApi.testError({}).then((value) {
                      Fluttertoast.showToast(msg: '需打开开发工具查看控制台');
                      print(value);
                    }).whenComplete(() {
                      LoadingTip.remove();
                    });
                  },
                ),
                ElevatedButton(
                  child: Text('test upload'),
                  onPressed: () async {
                    List<AssetEntity> list = await ChooseAssets.choose(
                      context: context,
                    );

                    LoadingTip.show(context: context);

                    List<ResultData<String>> resArray =
                        await Future.wait<ResultData<String>>(
                      list.map((element) => upload(element)),
                    );

                    List<String> uploadSrcs = [];

                    for (int i = 0; i < resArray.length; i++) {
                      if (resArray[i].code == Code.SUCCESS) {
                        uploadSrcs.add(resArray[i].data!);
                      } else {
                        Fluttertoast.showToast(msg: resArray[i].msg!);
                        return;
                      }
                    }

                    setState(() {
                      if (uploadSrcs.length > 0) {
                        _uploadSrcs = uploadSrcs;
                      }
                    });

                    LoadingTip.remove();
                  },
                ),
                ...List.generate(
                  _uploadSrcs.length,
                  (index) {
                    String url = _uploadSrcs[index];
                    return GestureDetector(
                      onTap: () {
                        _preview(index);
                      },
                      child: Container(
                        margin: EdgeInsets.all(10),
                        child: Hero(
                          tag: url + index.toString(),
                          child: ExtendedImage.network(
                            url,
                            width: 400 * Utils.rpx,
                            fit: BoxFit.fitWidth,
                          ),
                        ),
                      ),
                    );
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
